<?php
namespace Homework8;

class Graph {
	
	private $nodes;
	private $start;
	
	public function __construct(Node $start = null) {
		$this->start = $start;
	}
	
	public function __clone() {
		$this->start = clone $this->start;
		
		$nodes = array();
		foreach ($this->nodes as $node) {
			$nodes[] = clone $node;
		}
		$this->nodes = $nodes;
	}
	
	public function addNode(Node $node) {
		if (!isset($this->nodes[$node->getLabel()])) {
			$this->nodes[$node->getLabel()] = $node;
		}
		
		return $this;
	}
	
	public function isNodeInserted($label) {
		
		return isset($this->nodes[$label]);
	}
	
	public function setStart(Node $node) {
		if (!isset($this->nodes[$node->getLabel()])) {
			$this->addNode($node);
		}
		$this->start = $this->nodes[$node->getLabel()];
		
		return $this;
	}
	
	public function setStartByLabel($label) {
		if (!isset($this->nodes[$label])) {
			$this->start = $this->nodes[$label];
		}

		return $this;
	}
	
	public function getStart() {
		
		return $this->start;
	}
	
	public function getNode($label) {
		if (isset($this->nodes[$label])) {
			
			return $this->nodes[$label];
		}

		return null;
	}
	
	
	public function getFirstNotVisitedNode() {
		foreach ($this->nodes as $label => $node) {
			if (!$node->isVisited()) {
				
				return $node;
			}
		}
		
		return null;
	}
}

